package com.atguigui.leetcode1;

/**
 * 1017.负二进制转换
 * Project: leetcode
 * Package: com.atguigui.leetcode1
 * Version: 1.0
 * <p>
 * Created by WJX on 2023/4/6 9:53
 */
public class P1017ConvertToBase2 {
    public static void main(String[] args) {
        Solution solution = new P1017ConvertToBase2().new Solution();
        // TO TEST
    }

    class Solution {
        public String baseNeg2(int n) {
            if (n == 0 || n == 1) {
                return String.valueOf(n);
            }
            StringBuilder res = new StringBuilder();
            while (n != 0) {
                int remainder = n & 1;
                res.append(remainder);
                n -= remainder;
                n /= -2;
            }

            return res.reverse().toString();
        }
    }
}
